package com.e3.web.core.client;

import com.e3.web.widget.client.MyDialog;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.widget.Component;
import com.google.gwt.user.client.ui.Widget;

public class GXTHelper {
	
	private static CommonMessages MESSAGES = CommonMessages.INSTANCE;
	
	private static String CLIENT_SESSION = "CLIENT_SESSION";

	public static void maskQuery(Component comp){
		mask(comp,MESSAGES.maskinfo_query());
	}
	
	public static void maskSave(Component comp){
		mask(comp,MESSAGES.maskinfo_save());
	}
	
	public static void maskDelete(Component comp){
		mask(comp,MESSAGES.maskinfo_delete());
	}
	
	public static void mask(Component comp,String message){
		getRootComponent(comp).mask(message);
	}
	
	public static void unmask(Component comp){
		Component component = getRootComponent(comp);
		if(component!=null){
			component.unmask();
		}
	}
	
	private static Component getRootComponent(Component comp){
		if(comp==null){
			return comp;
		}
		if(comp instanceof MyDialog){
			return comp;
		}
		Widget widget = comp.getParent();
		if(widget instanceof Component){
			return getRootComponent((Component)widget);
		}
		return comp;
	}
	
	public static String newLine(String s){
		return s + "<br />";
	}
	
	public static GxtClientSession getClientSession(){
		GxtClientSession session = Registry.get(CLIENT_SESSION);
		if(session == null){
			session = new GxtClientSession();
			Registry.register(CLIENT_SESSION, session);
		}
		return session;
	}
	
	public static void setClientSession(GxtClientSession session){
		Registry.register(CLIENT_SESSION, session);
	}
	
	public static boolean isSuperAdmin(){
		return getClientSession().isSuperAdmin();
	}
	
	public static boolean isLocalCorp(){
		return getClientSession().isLocalCorp();
	}
}
